//
//  MyDownloadViewController.m
//  Double
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "MyDownloadViewController.h"
#import "DownloadViewController.h"
#import "DownloadedViewController.h"

@interface MyDownloadViewController ()<UIScrollViewDelegate>

@end

@implementation MyDownloadViewController

- (void)dealloc
{
    [_segmented release];
    [_scroll release];
    [super dealloc];
}
- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createScroll];
    [self createSegment];
//    self.navigationItem.titleView = self.segmented;
    self.navigationItem.title = @"我的下载";
    // 给tableView设置一个系统的编辑按钮(右上角)
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationController.navigationBar.translucent = NO;
//    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    // Do any additional setup after loading the view.
}

#pragma mark 创建ScrollView

- (void)createScroll
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    DownloadViewController * download = [[DownloadViewController alloc]init];
    download.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:download];
    
    DownloadedViewController * downloaded = [[DownloadedViewController alloc]init];
    downloaded.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:downloaded];
    
    [self.scroll addSubview:download.view];
    [self.scroll addSubview:downloaded.view];
    
    [self.view addSubview:self.scroll];
    [download release];
    [downloaded release];
    [self.scroll release];
}
#pragma mark 滑动scrollView 改变偏移量
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scroll.contentOffset.x == 0) {
        self.segmented.selectedSegmentIndex = 0;
    } else if (self.scroll.contentOffset.x == self.scroll.frame.size.width){
        self.segmented.selectedSegmentIndex = 1;
    }
}
#pragma mark 创建UISegmentedControl 包括"下载中"和"已下载"
- (void)createSegment
{
    self.segmented = [[UISegmentedControl alloc] initWithItems:@[@"下载中", @"已下载"]];
    self.segmented.frame = CGRectMake(100, 10, [UIScreen mainScreen].bounds.size.width - 200 , 30);
    self.segmented.selectedSegmentIndex = 0;
    self.segmented.layer.cornerRadius = 20;
    [self.segmented addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    UIView *view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)] autorelease];
    [self.view addSubview:view];
    [view setBackgroundColor:[UIColor whiteColor]];
    [view addSubview:self.segmented];
    [self.segmented release];
    [view release];
}
#pragma mark 点击UIsegmentedControl 改变偏移量
- (void)segmentAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        self.scroll.contentOffset = CGPointMake(0, 0);
    } else if (segment.selectedSegmentIndex == 1)
    {
        self.scroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
